﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web;
using UploadPlugin.Models;
using System.IO;

namespace UploadPlugin.Controllers
{
    public class UploadController : ApiController
    {
        // GET api/upload
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/upload/5
        public string Get(int id)
        {
            return "value";
        }

       //HttpContext context = new HttpContext();
//HttpContext.Current.Request.File
     
        //// POST api/upload
        //public void Post(HttpPostedFileWrapper file)
        //{
            
        //    string path = @"D:\Temp\";

        //    if (file != null)
        //        file.SaveAs(path + file.FileName);

        //    //return RedirectToAction("Index");
        //}

        // POST api/upload/5
        public void Post()
        {
            string filePath = @"C:\temp1";
            var postedFile = HttpContext.Current.Request.Files[0];
            
            if (!Request.Headers.Contains("X-File-Name"))
            {
                string fileName = Path.GetFileName(postedFile.FileName);
                FileUpload.Upload(postedFile.InputStream, Path.Combine(filePath, fileName));
            }
            else
            {
                FileUpload.Upload(postedFile.InputStream, 
                                      Path.Combine(filePath, Path.Combine(filePath, Request.Headers.GetValues("X-File-Name").First())), 
                                      true);
            }
        }
    }
}
